package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.UserEntity;

import service.UserService;
import util.MD5;

public class RegisteServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public RegisteServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		String flag = request.getParameter("flag");
		String result = "";
		try {
			// 针对验证
			if (flag.equals("userName")) {
				String value = request.getParameter("value");
				if (UserService.checkUserByUserName(value)) {
					result = "HasRegisted";
				} else {
					result = "UserAvailable";
				}

			} else if (flag.equals("phone")) {
				String value = request.getParameter("value");
				if (UserService.checkUserByPhone(value)) {
					result = "HaveUsed";
				} else {
					result = "PhoneNumAvailable";
				}
			} else {// 针对注册
				String userName = request.getParameter("userName");
				String password = request.getParameter("password");
				String tel = request.getParameter("tel");
				String addr = request.getParameter("addr");
				
				//检查用户名和电话是否已被注册
				if (UserService.checkUserByUserName(userName)) {
					result = "HasRegisted";
					out.println(result);
					return ;
				}
				if (UserService.checkUserByPhone(tel)) {
					result = "HaveUsed";	
					out.println(result);
					return ;
				}

				UserEntity user = new UserEntity();
				user.setAddr(addr);
				user.setName(userName);
				user.setPassword(MD5.Encrypt(password));
				// user.setRegTime(new Date());
				user.setTel(tel);
				boolean addResult = false;
				try {
					addResult = UserService.addUser(user);
				} catch (SQLException e) {
					e.printStackTrace();
				}

				if (!addResult) {
					result = "failed";
				} else {
					// session.setAttribute("userName", userName);
					// session.setAttribute("password", MD5.Encrypt(password));
					result = "success";
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		out.println(result);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
